$(function() {
    $("input").focus(function(){
        $(".errorMessage").hide();
        $(".errorTips").html("");
    });
    $('body').on('keyup', 'input[name="mobile"], input[name="mobileCode"],input[name="password"], input[name="confirmPwd"]', function (event) {
        event = event || window.event;
        if (event && event.keyCode == 13) { //回车事件
            $('#registerBtn').click();
        }
    });
    var largeHeader = document.getElementById('wrap'),
        height = window.innerHeight;
        largeHeader.style.height = height+'px';

    $("body").on('click','#sendRand',function () {//发送验手机证码
        var _that = this;
        disabledSend(_that);
        var mobile = $("input[name='mobile']").val();
        sendCode(mobile,1,function () {
            $(_that).hide();
            $("#countdown").show();
            $("#countdown").text('(60s)后重新获取');
            var time = 60;
            var inter = setInterval(function () {
                time --;
                if(time<=0){
                    window.clearInterval(inter);
                    $(_that).show();
                    $("#countdown").hide();
                    $("#countdown").text('');
                    relieveSend(_that);
                }else{
                    $("#countdown").text('('+time+'s)后重新获取');
                }
            },1000);
        },_that);
    }).on('click',"#registerBtn",function () {//注册
        var mobile=$('input[name="mobile"]').val();
        var mobileCode=$('input[name="mobileCode"]').val();
        var password=$('input[name="password"]').val();
        var confirmPwd=$('input[name="confirmPwd"]').val();
        var status = $('#registrationProtocol:checked').val();
        if (status == undefined || status == 'undefined') {
            $(".errorMessage").show();
            $(".errorTips").html('请您在阅读注册协议后同意并勾选');
            return;
        }
        if(password !=null && $.trim(password) !=''){
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            password = encrypt.encrypt(password);
        }
        if(confirmPwd !=null && $.trim(confirmPwd) !=''){
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            confirmPwd = encrypt.encrypt(confirmPwd);
        }
        $.ajax({
            url:baseloaction+"/web/user/doRegister",
            data:{
                "mobile":mobile,
                "mobileCode":mobileCode,
                "password":password,
                "confirmPwd":confirmPwd,
                "sign":sign
            },
            dataType:"json",
            type:"post",
            success:function (result) {
                if(result.success==true){
                    var url = result.url;
                    if(url==null || $.trim(url)==''){
                        redirectURL(baseloaction +'/web/uc/course/user/index');
                    }else{
                        window.location.href = baseloaction + url;
                    }
                }else{
                    sign = result.keyMap.sign;
                    publicKey = result.keyMap.publicKey;
                    $(".errorMessage").show();
                    $(".errorTips").html(result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，注册失败');
            }
        });
    });
});